//
//  PCModifyNameController.m
//  PhotoCircle
//
//  Created by GL on 15/12/18.
//  Copyright © 2015年 wujungao. All rights reserved.
//

#import "PCModifyNameController.h"
#import "PCUserModel.h"
#import "PCMeStore.h"
#import "GMString.h"
#import "GMTipsHelper.h"


@interface PCModifyNameController ()

@property (weak, nonatomic) IBOutlet UITextField *textField;
@property (nonatomic, strong) PCUserModel *userModel;

@end

@implementation PCModifyNameController

-(PCUserModel *)userModel{
    _userModel = [[PCUserInfoManager sharedUserInfo] readUserInfo];
    return _userModel;
}

-(void)configUI{
    _textField.text = self.userModel.userName;
    //[_textField becomeFirstResponder];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self configUI];
    // Do any additional setup after loading the view.
}

#pragma mark -
#pragma mark -ButtonEvent
- (IBAction)backEvent:(UIButton *)sender {
    [GMTipsHelper showDismiss];
    [self.navigationController popViewControllerAnimated:YES];
}

- (IBAction)modifyNameEvent:(UIButton *)sender {
    //修改昵称
    if ([NSString isEmptyString:_textField.text]) {
        return;
    }
    
    [GMTipsHelper showLoading];
    NSDictionary *pDic = @{@"attr_name":@"name",@"attr_val":_textField.text,@"token":self.userModel.userToken};
    [[PCMeStore sharedMeStore] modifyUserName:pDic successHandler:^(NSInteger responCode){
        if (responCode == API_RESPONSE_OK) {
            [GMTipsHelper showSuccess:@"修改成功"];
            self.userModel.userName = _textField.text;
            [[PCUserInfoManager sharedUserInfo] saveUserInfo:self.userModel];
            [self performSelector:@selector(popViewController) withObject:nil afterDelay:0.8];
        }else{
            [GMTipsHelper showFail:@"修改失败"];
        }
    } errorHandler:^{
        [GMTipsHelper showBadNetwork];
    }];
}

-(void)popViewController
{
    [self backEvent:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
